<template>
  <div class="register">
    <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" class="register-form">
      <h3 class="title">注册--wms帐号</h3>
      <el-form-item prop="name">
        <el-input v-model="registerForm.name" type="text" auto-complete="off" placeholder="租户名称">
          <svg-icon slot="prefix" icon-class="edit" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="nickName">
        <el-input v-model="registerForm.nickName" type="text" auto-complete="off" placeholder="联系人">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input v-model="registerForm.phone" type="text" auto-complete="off" placeholder="联系电话">
          <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
            <el-form-item prop="confirmPwd">
        <el-input
          v-model="registerForm.confirmPwd"
          type="password"
          auto-complete="off"
          placeholder="再次确认密码"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="lock" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
      </el-form-item>
      <div class="to_register">
        <el-button type="warning" style="margin:0px 0px 0px 15px;" @click.native.prevent="toLogin">返回登录</el-button>
      </div>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>
<script>

export default {
  name: "Register",
  data() {
    // 检验手机号
    var checkMobile = (rule, value, callback) => {
      const regMobile = /^1[1356789]\d{9}$/
      if (regMobile.test(value)) return callback()
      callback(new Error('请提供合法的手机号'))
    }
    // 确认两次密码是否输入相同
    var checkPassword = (rule, value, callback) => {
          if (value === '') {
              callback(new Error('请再次输入密码'));
          } else if (value !== this.registerForm.password) {
              callback(new Error('两次输入密码不一致!'));
          } else {
              callback();
          }
      };
    return {
      registerForm: {},
      registerRules: {
        name: [
          { required: true, trigger: "blur", message: "租户名称不能为空" }
        ],
        nickName: [
          { required: true, trigger: "blur", message: "联系人不能为空" }
        ],
        phone: [
          { required: true, trigger: "blur", message: "联系电话不能为空" },
          { validator: checkMobile, trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        confirmPwd: [
          { required: true, trigger: "blur", message: "密码不能为空" },
          { validator: checkPassword, trigger: 'blur' }
        ],
      },
      loading: false,
      redirect: undefined
    }
  },
  created() {},
  methods: {
    handleRegister() {
      this.$refs.registerFormRef.validate(valid=> {
        if (valid) {
          this.loading = true;
          this.$store.dispatch("Register", this.registerForm).then(() => {
            this.msgSuccess("注册成功");
            this.$router.push("/Login").catch(()=>{});
          }).catch(() => {
            this.loading = false;
          });
        }
      })
    },
    toLogin() {
      this.$router.push("/Login").catch(()=>{});
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.to_register{
  float: right;
  margin-bottom: 15px;
}
</style>
